package com.xinhuo.demo.web;

import com.alibaba.fastjson.JSONObject;
import com.xinhuo.demo.entity.Student;
import com.xinhuo.demo.entity.Teacher;
import com.xinhuo.demo.service.StudentService;
import com.xinhuo.demo.service.TeacherService;
import com.xinhuo.demo.util.ResultMsg;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @Auther: zhangxinlin
 * @Date: 2019/5/15 16:16:14
 * @Description:
 */
@RestController
@RequestMapping("/base")
public class BaseController {
    @Autowired
    private StudentService studentService;
    @Autowired
    private TeacherService teacherService;

    @PostMapping("/addStudentAndTeacher")
    public ResultMsg addStudentAndTeacher(String studentName,String teacherName){
        //新增一个学生
        Student student = new Student();
        student.setName(studentName);
        studentService.save(student);
        //新增一个教师
        Teacher teacher = new Teacher();
        teacher.setName(teacherName);
        teacherService.save(teacher);
        return ResultMsg.ok();
    }

    @RequestMapping("/find")
    public ResultMsg find(){
        //查询student表所有的学生
        List<Student> studentList = studentService.list();
        //查询teacher表所有的教师
        List<Teacher> teacherList = teacherService.list();
        JSONObject data = new JSONObject();
        data.put("studentList",studentList);
        data.put("teacherList",teacherList);
        ResultMsg resultMsg = new ResultMsg();
        resultMsg.setData(data);
        return resultMsg;
    }
}
